package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysIntegral;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysSign;
import com.ruoyi.project.system.service.ISysConfigService;
import com.ruoyi.project.system.service.ISysIntegralService;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/mobileUser/sign/")
public class UserSignController {

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ISysSignService sysSignService;

    @Autowired
    private ISysMobileUserService sysMobileUserService;

     SysIntegral sysIntegral = new SysIntegral();

    @Autowired
    private ISysIntegralService sysIntegralService;
     /**
      * 用户签到
      * */
    @GetMapping("addSignIn")
    public AjaxResult addSingIn(SysSign sysSign){
        String value = (String) sysConfigService.selectConfigByKey("CONFIG_WORKER_RATIO");
        double integral = Double.parseDouble(value);
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysSign.setMobileUserId(mobileUserId);
        sysSign.setIntegral(integral);
        sysSign.setCreateTime(DateUtils.getNowDate());
        SysSign user = sysSignService.querySignDate(sysSign.getMobileUserId());
        if(user != null ){
            return AjaxResult.error("不可连续签到");
        }
        sysMobileUserService.changeIntegral(sysSign);
        sysSignService.insertSysSign(sysSign);
        sysIntegral.setSignId(sysSign.getSignId());
        sysIntegral.setMobileUserId(mobileUserId);
        sysIntegral.setIntegralType("1");
        sysIntegral.setIntegral(integral);
        sysIntegralService.insertSysIntegral(sysIntegral);
        return AjaxResult.success("签到成功");
    }
    /**
     * 用户查询签到
     * */
    @GetMapping("querySign")
    public AjaxResult querySign(SysSign sysSign){
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysSign.setMobileUserId(mobileUserId);
        return AjaxResult.success(sysSignService.selectSysSignList(sysSign));
    }
}
